/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import a4.Controller;
import a4.Main;
import java.util.Scanner;
import user.Administrator;
import user.Customer;
import user.Employee;
import user.GeneralUser;

public class UI {

    protected static Scanner input = new Scanner(System.in);
    protected static String choise = new String();
    protected GeneralUser user;
    
    public void mainMenu() {
        System.out.print("Welcome:");
        do {
            System.out.println("\n1.Probolh menu!\n2.Login\n3.Register\n4.Exit!");
            choise = input.next();
            if (choise.equals("1"))
                printProductList();
            else if (choise.equals("2"))
                login();
            else if (choise.equals("3"))
                register();
            else if (choise.equals("4"))
                Controller.exit();
            else
                System.out.print("\nParakalw epilekste mia apo tis dynates epiloges!");
        }
        while (true);
    }

    protected void login() {

        String email, password;
        GeneralUser user = null;

        System.out.println("Welcome Guest!");
        do {
            System.out.print("Enter your email: ");
            email = input.next();

            System.out.print("Enter your password: ");
            password = input.next();

            user = Controller.checkLogin(email, password);
            if (user == null)
                System.out.println("Wrong username and/or password!\nPlease try again:");
        }
        while (user == null);
        System.out.println("Welcome " + user.getEmail() + "!");
        user.setLoggedIn(true);
        if (user instanceof Customer){
            CustomerUI customerUI = new CustomerUI( (Customer) user );
            customerUI.customerMenu();
        }
        else if (user instanceof Employee){
            EmployeeUI employeeUI = new EmployeeUI( (Employee) user );
            employeeUI.employeeMenu();
        }
        else{
            AdminUI adminUI = new AdminUI( (Administrator) user );
            adminUI.adminMenu();
        }
    }

    protected void logout(){
    }
    
    protected void register() {
        String email,
                name,
                surname,
                password,
                telephone,
                address;

        do {
            System.out.print("\nPlease enter a valid name: ");
            name = input.next();
        }
        while (name.matches("[A-Z]"));
        do {
            System.out.print("\nPlease enter a valid surname:");
            surname = input.next();
        }
        while (surname.matches(""));
        do {
            System.out.print("Please enter a valid email:\n:");
            email = input.next();
        }
        while (email.matches(""));
        do {
            System.out.print("Password must be shorter than 10 characters and longer than three.\n"
                    + "Please enter a desired password: ");
            password = input.next();
        }
        while (password.matches(""));
        do {
            System.out.print("Please enter a valid phone number:\n:");
            telephone = input.next();
        }
        while (telephone.matches(""));
        do {
            System.out.print("Please enter your adress:\n:");
            address = input.next();
        }
        while (address.matches(""));

        Controller.addCustomer(name, surname, email, password, telephone, address);
    }

    protected void printOfferList() {
        for (int i = 0; i < Main.offerList.size(); ++i)
            System.out.printf("%d. %10s %.2f\n", i, Main.offerList.get(i).getItem(), Main.offerList.get(i).getPrice());
    }

    protected void printProductList() {
        if (Main.productList.size() == 0)
            System.out.print("O katalogos einai adeios");
        System.out.println("Katalogos proiontwn:");
        for (int i = 0; i < Main.productList.size(); ++i)
            System.out.println(Main.productList.get(i));
        return;
    }

    protected void printOrderList() {
        for (int i = 0; i < Main.orderList.size(); ++i)
            System.out.println(Main.orderList.get(i));
    }

}